from typing import List

from scipy.special import comb


# 错误用例65


class Solution:
    def numberOfArithmeticSlices(self, nums: List[int]) -> int:
        size = len(nums)

        dp = [{} for _ in range(size)]

        ans = 0

        for i in range(size):
            for j in range(i + 1, size):
                d = nums[j] - nums[i]
                if d in dp[i]:
                    dp[j][d] = dp[i].pop(d) + 1
                else:
                    dp[j][d] = 1

            while dp[i]:
                k, v = dp[i].popitem()
                if v >= 2:
                    if k != 0:  # 差不为0的情况
                        ans += (1 + (v - 1)) * (v - 1) // 2
                    else:  # 差为0的情况
                        v += 1
                        for m in range(3, v + 1):
                            ans += int(comb(v, m))

        return ans


if __name__ == "__main__":
    print(Solution().numberOfArithmeticSlices([2, 4, 6, 8, 10]))  # 7

    # 测试用例1
    print(Solution().numberOfArithmeticSlices([7, 7, 7, 7, 7]))  # 16

    # 测试用例65
    print(Solution().numberOfArithmeticSlices([2, 2, 3, 4]))  # 2
